﻿/*----------------------------------------------------------------
//Copyright (C) 2012 赵国伟
//文件功能描述：品牌管理客户端脚本
//创建人：赵国伟
//生成时间：2012/03/17 16:15:00
//----------------------------------------------------------------*/
var AjaxServerUrl = "/Manage/AS/GoodsCommon.aspx";
var ModelOfBrand;
var ModelOfIDNameObj;
var BrandID = 0;
var CheckOptionOfCategories = "";
var SWFUploader = null; //上传控件；(SwfUpload)
var SessionID = "";
var BrandLogoUrl = "";

///初始化
function Init() {
    BrandID = 0;
    BrandLogoUrl = "";
}

//执行品牌搜索；
function GoToSearch() {
    var url = "BrandsManage.aspx?";
    url += "keyWords=" + escape($("#txtKeyWords").val().trim());
    url += "&Status=" + escape($("#ddlStatusOfSearch").val());
    location.href = url;
}

//执行删除
function Delete(id) {
    if (!confirm("系统提示：您确定要删除该品牌信息！"))
        return;
    AjaxGet(AjaxServerUrl + "?Action=DeleteBrand&BrandID=" + id);
}

///检查表单；
function CheckForm() {
    if ($("#txtBrandName").val() == "") {
        alert("系统提示：请输入品牌名称！");
        $("#txtBrandName").focus();
        return false;
    }
    if ($("#txtBrandCompanyName").val() == "") {
        alert("系统提示：请输入品牌公司！");
        $("#txtBrandCompanyName").focus();
        return false;
    }
    //检查品牌LOGO是否上传。

    if (!IsNumber($("#txtOrderValue").val())) {
        alert("系统提示：排序值必须为数字！");
        $("#txtOrderValue").focus();
        return false;
    }
    return true;
}

//保存；
function Save() {
    $("#btnSave").attr("disabled", true);
    if (!CheckForm()) {
        $("#btnSave").attr("disabled", false);
        return;
    }
    var data = clone(ModelOfBrand);
    data.Brand._brandid = BrandID;
    data.Brand._brandname = $("#txtBrandName").val();
    data.Brand._brandcompanyname = $("#txtBrandCompanyName").val();
    data.Brand._brandurl = $("#txtBrandUrl").val();
    data.Brand._logo = BrandLogoUrl;
    // data.Brand._branddescription = $("#txtBrandDescription").val();
    data.Brand._isindexcommend = $("#chkIsIndexCommend").attr("checked");
    data.Brand._afterservice = $("#txtAfterService").val();
    data.Brand._ordervalue = $("#txtOrderValue").val();
    data.Brand._status = $("#ddlStatus").val();
    data.Brand._lastmodifydate = "/Date(" + CurrentDateStringToUTC() + ")/";
    data.Brand._createdate = "/Date(" + CurrentDateStringToUTC() + ")/";
    
    data.Categories = new Array();
    var categoryIds = GetCheckObjectValuesToArray("chkCategory");
    for (var i = 0; i < categoryIds.length; ++i) {
        var obj = clone(ModelOfIDNameObj);
        obj._id = categoryIds[i];
        obj._name = "";
        data.Categories.push(obj);
    }
    var url = AjaxServerUrl + "?Action=SaveBrand";
    var postData = "jsonData=" + JSON.stringify(eval(data));
    postData += "&BrandDescription=" + escape(GetEditorHTMLContents("txtBrandDescription"));
    AjaxPostEx(url, "btnSave", postData);  //更新数据；
    $("#btnSave").attr("disabled", false);

}

//绑定品牌信息到录入表单中；
function BindBrandToForm() {
    var url = AjaxServerUrl + "?action=GetBrand&BrandID=" + BrandID + "&tttt=" + Math.random();
    $.ajax(
    {
        type: "Get",
        url: url,
        dataType: "json",
        success: function (data) {
            if (data != null) {
                $("#txtBrandName").val(data.Brand._brandname);
                $("#txtBrandCompanyName").val(data.Brand._brandcompanyname);
                $("#txtBrandUrl").val(data.Brand._brandurl);
                //设置LOGO;
                if (data.Brand._logo + "" != "")
                    SetUploadShow(data.Brand._logo);
                $("#txtBrandDescription").val(data.Brand._branddescription);
                $("#txtAfterService").val(data.Brand._afterservice);
                $("#chkIsIndexCommend").attr("checked", data.Brand._isindexcommend);
                $("#txtOrderValue").val(data.Brand._ordervalue);
                $("#ddlStatus").val(data.Brand._status);

                if (data.Categories != null) {
                    var ids = new Array();
                    for (var i = 0; i < data.Categories.length; ++i) {
                        ids.push(data.Categories[i]._id);
                    }
                    SetCheckObjectDefaultValue("chkCategory", ids.join(","));
                }

            }
        }
    });
}

///添加界面；
function Add() {
    Init();
    GetPopupDiv(700, 800, "#e2e2e2", "添加品牌", GetHtmlForm());
    GetEditor("txtBrandDescription", "98%");
    SWFUploader = CreateSwfUploadObject("/Manage/AS/AjaxUpload.aspx?action=UploadFiles", SessionID, "*.jpg;*.gif;*.png", "图片文件", "spUploadBtn", "spBrandPic");
}

//编辑界面；
function Edit(id) {
    Init();
    BrandID = id;
    GetPopupDiv(700, 800, "#e2e2e2", "编辑品牌", GetHtmlForm());
    GetEditor("txtBrandDescription", "98%");
    SWFUploader = CreateSwfUploadObject("/Manage/AS/AjaxUpload.aspx?action=UploadFiles", SessionID, "*.jpg;*.gif;*.png", "图片文件", "spUploadBtn", "spBrandPic");
    BindBrandToForm();
}

///设置上传成功后的图片显示。
function SetUploadShow(fileUrl) {
    //往外赋值；
    $("#trBrandPic").show();
    BrandLogoUrl = fileUrl;
    $("#spBrandPic").html("<a href=\"" + fileUrl + "\" target=\"_blank\"><img src=\"" + fileUrl + "\" width=\"200\"></a> [ <a href=\"javascript:void(0);\" onclick=\"DeleteUpload();\" class=\"red\">删除</a> ]");
}

///品牌图片上传成功后的处理；
function uploadSuccess(file, fileUrl) {
    try {
        SetUploadShow(fileUrl);
    } catch (ex) {
        this.debug(ex);
    }
}

function DeleteUpload() {
    BrandLogoUrl = "";
    $("#spBrandPic").html("");
    $("#trBrandPic").hide();
}


///获取录入界面的HTML;
function GetHtmlForm() {
    var html = new Array();
    html.push("<form>");
    html.push("<table width=\"700\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table\">");
    html.push("  <tr>");
    html.push("    <td colspan=\"2\" class=\"TableHeadRow\">&gt;&gt;品牌信息</td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td width=\"95\" align=\"right\">品牌名称：</td>");
    html.push("    <td><input name=\"txtBrandName\" type=\"text\"  class=\"border6\" id=\"txtBrandName\" size=\"50\" />");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">品牌公司：</td>");
    html.push("    <td><input name=\"txtBrandCompanyName\" type=\"text\"  class=\"border6\" id=\"txtBrandCompanyName\" size=\"50\" />");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td  align=\"right\">品牌网址：</td>");
    html.push("    <td ><input name=\"txtBrandUrl\" type=\"text\"  class=\"border6\" id=\"txtBrandUrl\" value=\"http://\" size=\"50\">");
    html.push("    <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">品牌LOGO：</td>");
    html.push("    <td><span id=\"spUploadBtn\"></span></td>");
    html.push("  </tr>");
    html.push("  <tr id=\"trBrandPic\" style=\"display:none;\">");
    html.push("    <td align=\"right\">&nbsp;</td>");
    html.push("    <td><span id=\"spBrandPic\"></span>&nbsp;</td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">品牌介绍：</td>");
    html.push("    <td><textarea name=\"txtBrandDescription\" cols=\"60\" rows=\"4\" class=\"border6\" id=\"txtBrandDescription\"></textarea></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">品牌售后：</td>");
    html.push("    <td><textarea name=\"txtAfterService\" cols=\"84\" rows=\"3\" class=\"border6\" id=\"txtAfterService\"></textarea></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">所属分类：</td>");
    html.push("    <td>");
    html.push(CheckOptionOfCategories);
    html.push("    </td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">&nbsp;</td>");
    html.push("    <td><label for=\"chkIsIndexCommend\"><input type=\"checkbox\" name=\"chkIsGroup\" value=\"1\" id=\"chkIsIndexCommend\" />是否首页推荐</label>");
    html.push("   </td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">排序值：</td>");
    html.push("    <td><input name=\"txtOrderValue\" type=\"text\" class=\"border6\" id=\"txtOrderValue\" value=\"0\" size=\"5\">");
    html.push("    <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">状态：</td>");
    html.push("    <td><select name=\"ddlStatus\" id=\"ddlStatus\">");
    html.push("      <option value=\"正用\">正用</option>");
    html.push("      <option value=\"停用\">停用</option>");
    html.push("                </select></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">&nbsp;</td>");
    html.push("    <td><input name=\"btnSave\" type=\"button\" id=\"btnSave\" value=\" 保 存 \" class=\"btn2\" onclick=\"Save();\">");
    html.push("    <input name=\"btnReset\" type=\"reset\" id=\"btnReset\" value=\" 重 置 \" class=\"btn2\">");
    html.push("    <input name=\"btnClose\" type=\"button\" id=\"btnClose\" value=\"关闭本窗口\" class=\"btn2\" onclick=\"yesOther(false);\"></td>");
    html.push("  </tr>");
    html.push("</table>");
    html.push("</form>");

    return html.join("");

}




function yesOther(t) {
    if (SWFUploader != null) {
        SWFUploader.destroy();
    }
    var IsHavePopDiv = false;
    var lastDivName = "";
    var divs = document.getElementsByTagName("DIV");
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].id.indexOf(prePopDivName) >= 0) {
            fullPopDivName = divs[i].id;
        }
    }
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].id.indexOf(prePopDivName) >= 0 && divs[i].id != fullPopDivName) {
            oo(divs[i].id).style.display = "";
            IsHavePopDiv = true;
        }
    }
    if (!t) {
        var bgObj2 = document.getElementById(fullPopDivName);
        document.body.removeChild(bgObj2);
        if (!IsHavePopDiv) {
            var bgObj = document.getElementById("bgDiv");
            document.body.removeChild(bgObj);
        }
    }
    else {
        var bgObj2 = document.getElementById(fullPopDivName);
        document.body.removeChild(bgObj2);
        if (!IsHavePopDiv) {
            var bgObj = document.getElementById("bgDiv");
            document.body.removeChild(bgObj);
        }
    }
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].id.indexOf(prePopDivName) >= 0) {
            fullPopDivName = divs[i].id;
        }
    }
}









